<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MatchAll 匹配所有项</title>
</head>

<body>
    <script>
        let text
        let regexp
        let results
        // 译者注：match() 方法
        text = "From 2019.01.29 to 2019.01.30";
        regexp = /(?<year>\d{4}).(?<month>\d{2}).(?<day>\d{2})/gu;
        results = text.match(regexp);
        console.log(results);
        // [ '2019.01.29', '2019.01.30' ]
        // 译者注：matchAll() 方法，可以看到结果的 groups 为命名捕获组
        text = "From 2019.01.29 to 2019.01.30";
        regexp = /(?<year>\d{4}).(?<month>\d{2}).(?<day>\d{2})/gu;
        results = Array.from(text.matchAll(regexp));
        console.log(results);
        // [
        //   [
        //     '2019.01.29',
        //     '2019',
        //     '01',
        //     '29',
        //     index: 5,
        //     input: 'From 2019.01.29 to 2019.01.30',
        //     groups: [Object: null prototype] { year: '2019', month: '01', day: '29' }
        //   ],
        //   [
        //     '2019.01.30',
        //     '2019',
        //     '01',
        //     '30',
        //     index: 19,
        //     input: 'From 2019.01.29 to 2019.01.30',
        //     groups: [Object: null prototype] { year: '2019', month: '01', day: '30' }
        //   ]
        // ]
    </script>
</body>

</html>